﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "GbmpSwitchViewDlg.h"
#include "GmStyleSheetHelper.h"
#include "IModelView.h"
#include "GbmpModelViewTypes.h"
#include "IUiDocument.h"

#include <QListWidgetItem>
#include "IElementBasicInformation.h"
#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"

GbmpSwitchViewDlg::GbmpSwitchViewDlg(std::vector<const gcmp::IModelView*>& arrView, QWidget *parent)
    : QDialog(parent,Qt::FramelessWindowHint)
{
    m_ui.setupUi(this);
    for each(const gcmp::IModelView* pView in arrView)
    {
        DBG_WARN_AND_CONTINUE_UNLESS(pView, L"pView指针怎么会是null？",L"GDMPLab",L"2024-03-30");
        std::wstring viewTypeName = gcmp::GbmpModelViewTypeUtil::GetModelViewTypeName(pView->GetUserDataId());

        std::wstring viewName = viewTypeName + L" - "+ pView->GetBasicInformation()->GetName();
        gcmp::OwnerPtr<QListWidgetItem> opItem = NEW_AS_OWNER_PTR(QListWidgetItem, QString::fromStdWString(viewName));
        opItem->setData(Qt::UserRole, pView->GetBasicInformation()->GetElementId().AsInt64());
        m_ui.m_viewList->addItem(TRANSFER_OWNERSHIP_TO_THIRD_PARTY(opItem));
    }
    m_ui.m_viewList->setCurrentRow(0);

    std::wstring styleSheetStr = GmStyleSheetHelper::Instance()->GetCurrentStyleData();
    setStyleSheet(QString::fromStdWString(styleSheetStr));
    m_ui.gbmp_title->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
    m_ui.gbmp_title->setIconVisible(false);
    m_ui.gbmp_title->setMaxButtonVisible(false);
    m_ui.gbmp_title->setMinButtonVisible(false);
    m_ui.gbmp_title->setText(QString::fromStdWString(GBMP_TR(L"转到视图")));
    this->setProperty("use_gbmp_title", true);
    connect(m_ui.gbmp_title, &GmTitleBar::closeRequested, this, &GbmpSwitchViewDlg::reject);
    connect(m_ui.gbmp_title, &GmTitleBar::moveRequested, this, &GbmpSwitchViewDlg::onMoveRequested);
    connect(m_ui.m_bnCancel,SIGNAL(clicked()), this, SLOT(reject()));
    connect(m_ui.m_bnOpenView, SIGNAL(clicked()), this, SLOT(OnClick_openView()));
}


GbmpSwitchViewDlg::~GbmpSwitchViewDlg()
{
}

void GbmpSwitchViewDlg::OnClick_openView()
{
    QModelIndex qModelIndex = m_ui.m_viewList->currentIndex();
    QListWidgetItem* pQListWidgetItem = m_ui.m_viewList->item(qModelIndex.row());

    gcmp::ElementId viewId(pQListWidgetItem->data(Qt::UserRole).toLongLong());
    DBG_WARN_UNLESS(viewId.IsValid(), L"视图Id非法",L"GDMPLab",L"2024-03-30");
    m_selectedViewId = viewId;

    accept();
}

void GbmpSwitchViewDlg::OnDoubleClicked_OpenView(QListWidgetItem *item)
{
    m_ui.m_viewList->setCurrentItem(item);
    OnClick_openView();
}

void GbmpSwitchViewDlg::onMoveRequested(const QPoint& cursorPos, const QPoint& offset)
{
    Q_UNUSED(cursorPos);
    move(pos() + offset);
}

